home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / PrefConnect.cpp < prev    next >
C/C++ Source or Header  |  2000-06-24  |  11KB  |  462 lines

  1. /********************************************************************************
  2.  
  3.     Gnucleus - A node application for the Gnutella network
  4.     Copyright (C) 2000 John Marshall
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     For support, questions, comments, etc...
  20.     E-Mail: 
  21.         swabby@c0re.net
  22.     
  23.     Address:
  24.         21 Cadogan Way
  25.         Nashua, NH, USA 03062
  26.  
  27. ********************************************************************************/
  28.  
  29. // PrefConnect.cpp : implementation file
  30. //
  31.  
  32. #include "stdafx.h"
  33. #include "Gnucleus.h"
  34. #include "GnucleusDoc.h"
  35. #include "PrefConnect.h"
  36.  
  37. #include "ViewSearch.h"
  38.  
  39. #include "GnuSock.h"
  40. #include "GnuHash.h"
  41. #include "GnuControl.h"
  42.  
  43. #ifdef _DEBUG
  44. #define new DEBUG_NEW
  45. #undef THIS_FILE
  46. static char THIS_FILE[] = __FILE__;
  47. #endif
  48.  
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CPrefConnect property page
  51.  
  52. IMPLEMENT_DYNCREATE(CPrefConnect, CPropertyPage)
  53.  
  54. CPrefConnect::CPrefConnect() : CPropertyPage(CPrefConnect::IDD)
  55. {
  56.     Doc = NULL;
  57.  
  58.     //{{AFX_DATA_INIT(CPrefConnect)
  59.         // NOTE: the ClassWizard will add member initialization here
  60.     //}}AFX_DATA_INIT
  61. }
  62.  
  63. CPrefConnect::~CPrefConnect()
  64. {
  65. }
  66.  
  67. void CPrefConnect::DoDataExchange(CDataExchange* pDX)
  68. {
  69.     CPropertyPage::DoDataExchange(pDX);
  70.     //{{AFX_DATA_MAP(CPrefConnect)
  71.     DDX_Control(pDX, IDC_CHECK_DROP, m_chkDrop);
  72.     DDX_Control(pDX, IDC_EDIT_TIMEOUT, m_ebTimeout);
  73.     DDX_Control(pDX, IDC_EDIT_KEEP, m_ebKeep);
  74.     DDX_Control(pDX, IDC_CHECK_KEEP_CONNECT, m_chkKeep);
  75.     DDX_Control(pDX, IDC_EDIT_IP, m_ebIP);
  76.     DDX_Control(pDX, IDC_CHECK_IP, m_chkIP);
  77.     DDX_Control(pDX, IDC_CHECK_SPEED, m_chkSpeed);
  78.     DDX_Control(pDX, IDC_CHECK_PORT, m_chkPort);
  79.     DDX_Control(pDX, IDC_EDIT_PORT, m_ebPort);
  80.     DDX_Control(pDX, IDC_COMBO_SPEED, m_cmbSpeed);
  81.     //}}AFX_DATA_MAP
  82. }
  83.  
  84.  
  85. BEGIN_MESSAGE_MAP(CPrefConnect, CPropertyPage)
  86.     //{{AFX_MSG_MAP(CPrefConnect)
  87.     ON_BN_CLICKED(IDC_CHECK_PORT, OnCheckIncomingPort)
  88.     ON_EN_CHANGE(IDC_EDIT_PORT, OnChangeEditIncomingPort)
  89.     ON_BN_CLICKED(IDC_CHECK_SPEED, OnCheckSpeed)
  90.     ON_CBN_EDITCHANGE(IDC_COMBO_SPEED, OnEditchangeComboSpeed)
  91.     ON_BN_CLICKED(IDC_CHECK_IP, OnCheckIp)
  92.     ON_EN_CHANGE(IDC_EDIT_IP, OnChangeEditIp)
  93.     ON_BN_CLICKED(IDC_CHECK_KEEP_CONNECT, OnCheckKeepConnect)
  94.     ON_EN_CHANGE(IDC_EDIT_KEEP, OnChangeEditKeep)
  95.     ON_EN_CHANGE(IDC_EDIT_TIMEOUT, OnChangeEditTimeout)
  96.     ON_BN_CLICKED(IDC_CHECK_DROP, OnCheckDrop)
  97.     //}}AFX_MSG_MAP
  98. END_MESSAGE_MAP()
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CPrefConnect message handlers
  102.  
  103. BOOL CPrefConnect::OnInitDialog() 
  104. {
  105.     CPropertyPage::OnInitDialog();
  106.     
  107.     Doc = (CGnucleusDoc *) ((CGnucleusApp *) AfxGetApp())->MasterDoc;
  108.  
  109.     // Port selection
  110.     if(Doc->m_ConstPort == 0)
  111.     {
  112.         m_chkPort.SetCheck(0);
  113.         m_ebPort.EnableWindow(FALSE);
  114.         m_ebPort.SetWindowText("Random");
  115.     }
  116.     else
  117.     {
  118.         m_chkPort.SetCheck(1);
  119.         m_ebPort.SetWindowText( DWrdtoStr(Doc->m_ConstPort));
  120.     }
  121.  
  122.     // Speed selection
  123.     m_cmbSpeed.InsertString(0, "T3 (or Greater)");
  124.     m_cmbSpeed.InsertString(0, "T1");
  125.     m_cmbSpeed.InsertString(0, "DSL");
  126.     m_cmbSpeed.InsertString(0, "Cable");
  127.     m_cmbSpeed.InsertString(0, "ISDN-128K");
  128.     m_cmbSpeed.InsertString(0, "ISDN-56K");
  129.     m_cmbSpeed.InsertString(0, "56K Modem");
  130.     m_cmbSpeed.InsertString(0, "33.6 Modem");
  131.     m_cmbSpeed.InsertString(0, "28.8 Modem");
  132.     m_cmbSpeed.InsertString(0, "14.4 Modem");
  133.     m_cmbSpeed.InsertString(0, "");
  134.  
  135.     if(Doc->m_ConnectSpeed)
  136.     {
  137.         m_chkSpeed.SetCheck(1);
  138.         CString speed_string = GetSpeedString(Doc->m_ConnectSpeed);
  139.         m_cmbSpeed.SetWindowText( speed_string.IsEmpty() ? DWrdtoStr(Doc->m_ConnectSpeed) : speed_string);
  140.     }
  141.     else
  142.     {
  143.         m_chkSpeed.SetCheck(0);
  144.         m_cmbSpeed.EnableWindow(FALSE);
  145.         m_cmbSpeed.SetWindowText( DWrdtoStr(Doc->m_EstSpeed) );
  146.     }
  147.  
  148.     // Local IP
  149.     if(Doc->m_ForceIP.a == 0 && Doc->m_ForceIP.b == 0 && Doc->m_ForceIP.c == 0 && Doc->m_ForceIP.d == 0)
  150.     {
  151.         m_chkIP.SetCheck(0);
  152.         m_ebIP.EnableWindow(FALSE);
  153.         m_ebIP.SetWindowText( Doc->GnuComm->localHost );
  154.     }
  155.     else
  156.     {
  157.         m_chkIP.SetCheck(1);
  158.         m_ebIP.SetWindowText( IPtoStr(Doc->m_ForceIP) );
  159.     }
  160.  
  161.     // Connect preference
  162.     if(Doc->m_MonitorType == 0)
  163.     {
  164.         m_chkKeep.SetCheck(0);
  165.         
  166.         ((CButton *) GetDlgItem(IDC_RADIO_LESS))->EnableWindow(FALSE);
  167.         ((CButton *) GetDlgItem(IDC_RADIO_EXACT))->EnableWindow(FALSE);
  168.         ((CButton *) GetDlgItem(IDC_RADIO_MORE))->EnableWindow(FALSE);
  169.  
  170.         ((CButton *) GetDlgItem(IDC_RADIO_MORE))->SetCheck(1);
  171.  
  172.         m_ebKeep.SetWindowText( DWrdtoStr(Doc->m_ConnectNum) );
  173.         m_ebKeep.EnableWindow(FALSE);
  174.     }
  175.     else
  176.     {
  177.         m_chkKeep.SetCheck(1);
  178.  
  179.         if(Doc->m_MonitorType == 1)
  180.             ((CButton *) GetDlgItem(IDC_RADIO_LESS))->SetCheck(1);
  181.         else if(Doc->m_MonitorType == 2)
  182.             ((CButton *) GetDlgItem(IDC_RADIO_EXACT))->SetCheck(1);
  183.         else if(Doc->m_MonitorType == 3)
  184.             ((CButton *) GetDlgItem(IDC_RADIO_MORE))->SetCheck(1);
  185.         else
  186.             ((CButton *) GetDlgItem(IDC_RADIO_MORE))->SetCheck(1);
  187.  
  188.         m_ebKeep.SetWindowText( DWrdtoStr(Doc->m_ConnectNum));
  189.     }
  190.     
  191.     // Timeout length
  192.     m_ebTimeout.SetWindowText( DWrdtoStr(Doc->m_TimeoutConnect) );
  193.  
  194.     // Drop established for incoming
  195.     m_chkDrop.SetCheck(Doc->m_DropForIncoming);
  196.  
  197.  
  198.     return TRUE;  // return TRUE unless you set the focus to a control
  199.                   // EXCEPTION: OCX Property Pages should return FALSE
  200. }
  201.  
  202. void CPrefConnect::OnCheckIncomingPort() 
  203. {
  204.     if(m_chkPort.GetCheck())
  205.     {
  206.         m_ebPort.EnableWindow();
  207.         m_ebPort.SetWindowText("");
  208.     }
  209.     else
  210.     {
  211.         m_ebPort.EnableWindow(FALSE);
  212.         m_ebPort.SetWindowText("Random");
  213.     }
  214.  
  215.     SetModified();    
  216. }
  217.  
  218. void CPrefConnect::OnChangeEditIncomingPort() 
  219. {
  220.     SetModified();
  221. }
  222.  
  223. void CPrefConnect::OnCheckSpeed() 
  224. {
  225.     if(m_chkSpeed.GetCheck())
  226.     {
  227.         m_cmbSpeed.EnableWindow();
  228.         m_cmbSpeed.SetWindowText("");
  229.     }
  230.     else
  231.     {
  232.         m_cmbSpeed.EnableWindow(FALSE);
  233.         m_cmbSpeed.SetWindowText( DWrdtoStr(Doc->m_EstSpeed) );
  234.     }
  235.  
  236.     SetModified();
  237. }
  238.  
  239. void CPrefConnect::OnEditchangeComboSpeed() 
  240. {
  241.     SetModified();
  242. }
  243.  
  244. void CPrefConnect::OnCheckIp() 
  245. {
  246.     if(m_chkIP.GetCheck())
  247.     {
  248.         m_ebIP.EnableWindow();
  249.         m_ebIP.SetWindowText("");
  250.     }
  251.     else
  252.     {
  253.         m_ebIP.EnableWindow(FALSE);
  254.         m_ebIP.SetWindowText( Doc->GnuComm->localHost );
  255.     }
  256.  
  257.     SetModified();
  258. }
  259.  
  260. void CPrefConnect::OnChangeEditIp() 
  261. {
  262.     SetModified();    
  263. }
  264.  
  265. void CPrefConnect::OnCheckKeepConnect() 
  266. {
  267.     if(m_chkKeep.GetCheck())
  268.     {
  269.         ((CButton *) GetDlgItem(IDC_RADIO_LESS))->EnableWindow();
  270.         ((CButton *) GetDlgItem(IDC_RADIO_EXACT))->EnableWindow();
  271.         ((CButton *) GetDlgItem(IDC_RADIO_MORE))->EnableWindow();
  272.  
  273.         m_ebKeep.EnableWindow();
  274.     }
  275.     else
  276.     {
  277.         ((CButton *) GetDlgItem(IDC_RADIO_LESS))->EnableWindow(FALSE);
  278.         ((CButton *) GetDlgItem(IDC_RADIO_EXACT))->EnableWindow(FALSE);
  279.         ((CButton *) GetDlgItem(IDC_RADIO_MORE))->EnableWindow(FALSE);
  280.  
  281.         m_ebKeep.EnableWindow(FALSE);
  282.     }
  283.  
  284.     SetModified();
  285. }
  286.  
  287. void CPrefConnect::OnChangeEditKeep() 
  288. {
  289.     SetModified();
  290. }
  291.  
  292. void CPrefConnect::OnChangeEditTimeout() 
  293. {
  294.     SetModified();    
  295. }
  296.  
  297. void CPrefConnect::OnCheckDrop() 
  298. {
  299.     SetModified();
  300. }
  301.  
  302. BOOL CPrefConnect::OnApply() 
  303. {
  304.     CString store;
  305.  
  306.     // Port selection
  307.     if(m_chkPort.GetCheck())
  308.     {
  309.         m_ebPort.GetWindowText(store);
  310.         
  311.         if( atol(store) )
  312.             Doc->m_ConstPort = (DWORD) atol(store);
  313.  
  314.         if(Doc->m_ConstPort != Doc->GnuComm->localPort)
  315.         {
  316.             Doc->GnuComm->localPort = Doc->m_ConstPort;
  317.             Doc->GnuComm->StartListening();
  318.         }
  319.     }
  320.     else
  321.     {
  322.         if(Doc->m_ConstPort)
  323.         {
  324.             Doc->GnuComm->localPort = rand() % 22500 + 2500;
  325.             Doc->GnuComm->StartListening();
  326.         }
  327.  
  328.         Doc->m_ConstPort = 0;
  329.     }
  330.     
  331.     // Speed Selection
  332.     if(m_chkSpeed.GetCheck())
  333.     {
  334.         m_cmbSpeed.GetWindowText(store);
  335.  
  336.         Doc->m_ConnectSpeed = GetSpeedinBytes(store);
  337.     }
  338.     else
  339.         Doc->m_ConnectSpeed = 0;
  340.  
  341.     // IP Selection
  342.     if(m_chkIP.GetCheck())
  343.     {
  344.         m_ebIP.GetWindowText(store);
  345.         
  346.         Doc->m_ForceIP = StrtoIP(store);
  347.     }
  348.     else
  349.     {
  350.         Doc->m_ForceIP.a = 0;
  351.         Doc->m_ForceIP.b = 0;
  352.         Doc->m_ForceIP.c = 0;
  353.         Doc->m_ForceIP.d = 0;
  354.     }
  355.  
  356.     // Connect preference
  357.     if(m_chkKeep.GetCheck())
  358.     {
  359.         if(    ((CButton *) GetDlgItem(IDC_RADIO_LESS))->GetCheck() )
  360.             Doc->m_MonitorType = 1;
  361.         else if( ((CButton *) GetDlgItem(IDC_RADIO_EXACT))->GetCheck() )
  362.             Doc->m_MonitorType = 2;
  363.         else if( ((CButton *) GetDlgItem(IDC_RADIO_MORE))->GetCheck() )
  364.             Doc->m_MonitorType = 3;
  365.         else
  366.             Doc->m_MonitorType = 0;
  367.  
  368.         m_ebKeep.GetWindowText(store);
  369.         Doc->m_ConnectNum = atoi(store);
  370.     }
  371.     else
  372.     {
  373.         Doc->m_MonitorType = 0;
  374.     }
  375.  
  376.     // Timeout length
  377.     m_ebTimeout.GetWindowText(store);
  378.  
  379.     if( atoi(store) )
  380.         Doc->m_TimeoutConnect = atoi(store);
  381.     else
  382.         Doc->m_TimeoutConnect = 4;
  383.  
  384.     // Drop established for incoming
  385.     if(m_chkDrop.GetCheck())
  386.         Doc->m_DropForIncoming = 1;
  387.     else
  388.         Doc->m_DropForIncoming = 0;
  389.  
  390.     return CPropertyPage::OnApply();
  391. }
  392.  
  393. DWORD CPrefConnect::GetSpeedinBytes(CString Speed)
  394. // The protocol is messed, bytes are bits 
  395. {
  396.     if(Speed == "")
  397.         return 0;
  398.     if(Speed == "14.4 Modem")
  399.         return 14;
  400.     if(Speed == "28.8 Modem")
  401.         return 29;
  402.     if(Speed == "33.6 Modem")
  403.         return 33;
  404.     if(Speed == "56K Modem")
  405.         return 53;
  406.     if(Speed == "ISDN-56K")
  407.         return 56;
  408.     if(Speed == "ISDN-128K")
  409.         return 128;
  410.     if(Speed == "Cable")
  411.         return 384;
  412.     if(Speed == "DSL")
  413.         return 768;
  414.     if(Speed == "T1")
  415.         return 1500;
  416.     if(Speed == "T3 (or Greater)")
  417.         return 45000;
  418.     
  419.     return atol(Speed);
  420. }
  421.  
  422. CString CPrefConnect::GetSpeedString(DWORD _dw_speed)
  423. // The protocol is messed, bytes are bits 
  424. {
  425.     // limit to only those speeds returned by GetSpeedinBytes
  426.     switch(_dw_speed)
  427.     {
  428.     case 14:
  429.         return "14.4 Modem";
  430.     case 29:
  431.         return  "28.8 Modem";
  432.     case 33:
  433.         return "33.6 Modem";
  434.     case 53:
  435.         return "56K Modem";
  436.     case 56:
  437.         return "ISDN-56K";
  438.     case 128:
  439.         return "ISDN-128K";
  440.     case 384:
  441.         return "Cable";
  442.     case 768:
  443.         return "DSL";
  444.     case 1500:
  445.         return "T1";
  446.     case 45000:
  447.         return "T3 (or Greater)";
  448.     default:
  449.         return "";
  450.     }
  451. }
  452.  
  453. /*
  454.  
  455.     
  456.     // Timeout length
  457.     m_ebTimeout.SetWindowText( DWrdtoStr(Doc->m_TimeoutConnect) );
  458.  
  459.     // Drop established for incoming
  460.     m_chkDrop.SetCheck(Doc->m_DropForIncoming);
  461. */
  462.